<template>
  <div>
    <Header></Header>
    <section>
      <Routers></Routers>
      <div id="section" class="main">
        <div class="common_header">
          <span class="common_header_tip">长期待摊费用</span>
          <div>
            <!--<button class="common_header_btn" @click="synchronization">同步数据</button>-->
            <select class="common_header_select" v-model="search_type" @change="search">
              <option value="">全部</option>
              <option :value="item.id" v-for="(item, index) in typeList">{{item.name}}</option>
            </select>
            <button class="common_header_btn" @click="add">添加</button>
            <button class="common_header_btn" @click="imports">批量导入</button>
            <button class="common_header_btn" @click="batchDel">批量删除</button>
          </div>
        </div>

        <table class="common_table" cellspacing="0" cellpadding="0">
          <thead>
          <tr>
            <th><input type="checkbox" v-model='checked' @click='checkedAll'></th>
            <th>年月</th>
            <th>类型</th>
            <th>费用金额(元)</th>
            <th>操作</th>
          </tr>
          </thead>
          <tbody>
          <tr v-for="(item ,index) in list" :key="index">
            <td><input type="checkbox" v-model='checkList' :value='item.id'></td>
            <td><span>{{item.cost_time}}</span></td>
            <td><span>{{item.cost_type}}</span></td>
            <td><span>{{item.cost_money}}</span></td>
            <td class="operation_td">
              <span class="common_table_editBTN" @click="edit(item.id)">编辑</span>
              <span class="common_table_delBTN" @click="del(item.id)">删除</span>
            </td>
          </tr>
          </tbody>
        </table>

        <!--分页-->
        <ul class="pagination" >
          <li v-show="current != 1" @click="current-- && goto(current)" ><a href="#">上一页</a></li>
          <li v-for="index in pages" @click="goto(index)" :class="{'active':current == index}" :key="index">
            <a href="#" >{{index}}</a>
          </li>
          <li v-show="allpage != current && allpage != 0 " @click="current++ && goto(current++)"><a href="#" >下一页</a></li>
        </ul>
        <!--文字弹框 -->
        <div class="tankuang_box" v-if="tankuangISshow">
          <div class="tankuang">
            <p class="tankuangMSG">{{tankuangMSG}}</p>
            <button class="tankuang_ok_btn" @click="tankuangISshowBTN">确认</button>
          </div>
        </div>
        <!--批量导入弹框-->
        <div class="tankuang_box tankuang_box_import" v-if="importBoxISshow">
          <div class="tankuang">
            <i class="iconfont icon-guanbi tankuang_box_import_closeBTN" @click="closeImportBox"></i>
            <p class="tankuangMSG">温馨提示</p>
            <p class="tankuang_box_importMSG">请下载统一的模版，并按相应的格式在Excel软件中填写您的业务数据，然后再导入到系统中。</p>
            <input type="file" class="common_import" @change="import_file($event)">
            <p v-if="import_error" class="import_error_msg">{{import_error_msg}}</p>
            <div class="tankuang_btn_box">
              <!--<button class="common_import_ok_btn">下载模板</button>-->
              <a class="common_import_ok_btn" href="http://fys.youledi.cn/excel/批量导入长期待摊费用模板.xls">下载模板</a>
              <button class="common_import_cancel_btn" v-if="!comfirm_import">确定导入</button>
              <button class="common_import_ok_btn" v-if="comfirm_import" @click="comfirmImport($event)">确定导入</button>
            </div>
          </div>
        </div>

        <!--添加的弹框-->
        <div class="tankuang_box" v-if="boxISshow">
          <!--里面的白色盒子（需要自己设置宽高）-->
          <div class="add_tankuang">
            <!--顶部的文字和叉叉（通用）-->
            <div class="add_tankuang_header">
              <span>{{tankuang_title}}</span>
              <i class="iconfont icon-guanbi" @click="closeBox"></i>
            </div>
            <div class="add_tankuang_content">
              <div class="add_tankuang_content_box">
                <!--这里的内容是根据需要的内容加入（不通用）-->
                <ul class="common_tankuang_content">
                  <li>
                    <span>选择年月：</span>
                    <!--<input type="text" v-model="time">-->
                    <vue-datepicker-local v-model="time" format="YYYY-MM" class="datepicker"/>
                  </li>
                  <li>
                    <span>选择类型：</span>
                    <!--<input type="text">-->
                    <select v-model="type">
                      <option :value="item.name" v-for="(item, index) in typeList">{{item.name}}</option>
                    </select>
                  </li>
                  <li>
                    <span>费用金额(元)：</span>
                    <input type="text" v-model="money">
                  </li>
                </ul>
                <!--底部的按钮（通用）-->
                <div class="add_tankuang_btnBOX">
                  <button class="add_tankuang_btn add_tankuang_btn_first" @click="save">保存</button>
                  <button class="add_tankuang_btn" @click="cancelSave">取消</button>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!--删除确认框 -->
        <div class="tankuang_box" v-if="delISshow">
          <div class="tankuang">
            <p class="tankuangMSG">确认删除吗？</p>
            <div class="tankuang_btn_box">
              <button class="tankuang_ok_btn" @click="delBTN">确认</button>
              <button class="tankuang_ok_btn" @click="cancelDel">取消</button>
            </div>
          </div>
        </div>

        <!--同步数据弹出来的框框-->
        <div class="tankuang_box" v-show="isnodataShow">
          <div id="nodatabox">
            <p class="nodata_text">该企业暂无数据！</p>
            <p class="nodata_text">点击访问 <span class="nodata_text_link">科税通</span>了解详情</p>
            <a class="nodata_link_btn" href="http://www.rd.zhjjkc.com" target="_blank">去访问</a>
            <i class="iconfont icon-guanbi" @click="closeSynchronizationBOX"></i>
          </div>
        </div>

      </div>
    </section>
  </div>
</template>

<script>
  import Routers from '@/template/routers.vue'
  import Header from '@/template/common_header.vue'
  import VueDatepickerLocal from 'vue-datepicker-local'
  import { Toast } from 'mint-ui';
  export default {
    name: 'cost_daitan_detail',
    components: {
      Routers,
      Header,
      VueDatepickerLocal
    },
    data () {
      return {
        id:'',
        pid:null,
        list:[],
        typeList:[
          {id:1, name:'改建'},
          {id:2, name:'改装'},
          {id:3, name:'装修'},
          {id:4, name:'修理'}
        ],
        search_type:'',
        tankuangMSG:'',

        time:'',
        type:'',
        money:'',

        /*控制*/
        boxISshow:false,
        delISshow:false,
        importBoxISshow:false,
        tankuangISshow:false,
        isnodataShow:false,//同步数据点击了没，那个框是否出来
        /**批量导入**/
        importFile:'',
        comfirm_import:false,
        import_error:false,
        import_error_msg:'',
        /****全选单选反选****/
        checked:false,
        checkList:[],
        /***删除***/
        del_list:[],
        /****分页****/
        current:1,
        showItem:5,
        allpage:1
        /***********/
      }
    },
    computed:{
      pages:function(){
        let pag = [];
        if( this.current < this.showItem ){ //如果当前的激活的项 小于要显示的条数
          //总页数和要显示的条数那个大就显示多少条
          let i = Math.min(this.showItem,this.allpage);
          while(i){
            pag.unshift(i--);
          }
        }else{ //当前页数大于显示页数了
          let middle = this.current - Math.floor(this.showItem / 2 ),//从哪里开始
            i = this.showItem;
          if( middle >  (this.allpage - this.showItem)  ){
            middle = (this.allpage - this.showItem) + 1
          }
          while(i--){
            pag.push( middle++ );
          }
        }
        return pag
      }
    },
    watch: {//深度 watcher
      'checkList': {
        handler: function(val, oldVal) {
          if (val.length === this.list.length) {
            this.checked = true;
          } else {
            this.checked = false;
          }
        },
        deep: true
      }
    },
    mounted: function() {
      this.id = this.$route.params.id
      this.axios.get('/index/dknow_list?page=1&pro_id='+this.id).then((res)=>{
        if(res.data.code==1){
          this.list = res.data.data
          this.allpage = res.data.last_page
        }else{
          this.list = []
        }
      })
    },
    methods: {
      /*分页*/
      goto (index) {
        this.current = index;
        //这里可以发送ajax请求
        this.axios.get('/index/dknow_list?pro_id='+this.id+'&page='+this.current+'&cost_type='+this.search_type).then((res)=>{
          if(res.data.code==1){
            this.list = res.data.data
            this.allpage = res.data.last_page
          }else{
            this.list = []
          }
        })
      },
      /***全选*****/
      checkedAll () {
        let _this = this;
        if (!this.checked) {//实现全选
          _this.checkList = [];
          _this.list.forEach(function(item) {
            _this.checkList.push(item.id);
          });
        }else{//实现反选
          _this.checkList = [];
        }
      },
      /*删除*/
      del (id) {
        this.del_list = []
        this.del_list.push(id)
        this.delISshow = !this.delISshow
      },
      batchDel () {
        if(this.checkList.length<=0){
          this.tankuangMSG = '没有选择任何内容'
          this.tankuangISshow = !this.tankuangISshow
        }else{
          this.delISshow = !this.delISshow
        }
      },
      delBTN () {
//        console.log(1)
        if(this.del_list.length>0){
          this.axios.post('/index/del_dknow',{
            ids:JSON.stringify(this.del_list)
          }).then((res)=>{
            if(res.data.code==1){
              this.delISshow = !this.delISshow
              this.del_list = []
              this.axios.get('/index/dknow_list?pro_id='+this.id+'&page='+this.current+'&cost_type='+this.search_type).then((res)=>{
                if(res.data.code==1){
                  this.list = res.data.data
                  this.allpage = res.data.last_page
                }else{
                  this.list = []
                }
              })
            }else{
              Toast(res.data.msg)
              return
            }
          })
        }else{
          this.axios.post('/index/del_dknow',{
            ids:JSON.stringify(this.checkList)
          }).then((res)=>{
            if(res.data.code==1){
              this.delISshow = !this.delISshow
              this.checkList = []
              this.checked = false
              this.axios.get('/index/dknow_list?pro_id='+this.id+'&page='+this.current+'&cost_type='+this.search_type).then((res)=>{
                if(res.data.code==1){
                  this.list = res.data.data
                  this.allpage = res.data.last_page
                }else{
                  this.list = []
                }
              })
            }else{
              Toast(res.data.msg)
              return
            }
          })
        }
      },
      /*批量导入*/
      imports(){
        this.importBoxISshow = !this.importBoxISshow
        this.importFile = ''
      },
      closeImportBox () {
        this.importBoxISshow = !this.importBoxISshow
        this.importFile = ''
        this.comfirm_import = false
      },
      comfirmImport (event) {
        this.axios.post('/index/import_dknow',{
          dknows:this.importFile,
          pro_id:this.id
        }).then((res)=>{
          if(res.data.code==0){
            this.import_error = true
            this.import_error_msg = res.data.msg
          }else{
            this.axios.get('/index/dknow_list?pro_id='+this.id+'&page=1').then((res)=>{
              if(res.data.code==1){
                this.list = res.data.data
                this.allpage = res.data.last_page
                this.importBoxISshow = !this.importBoxISshow
                this.importFile = ''
                this.comfirm_import = false
              }else{
                this.list = []
              }
            })
          }
        })
      },
      import_file(e){
        let file = e.target.files[0]
        let reader = new FileReader()
        reader.readAsDataURL(file)
        let _this = this
        reader.onload = function(e) {
          _this.importFile = this.result
          _this.comfirm_import = !_this.comfirm_import
        }
      },
      /*搜索*/
      search () {
        this.axios.get('/index/dknow_list?page=1&pro_id='+this.id+'&cost_type='+this.search_type).then((res)=>{
          if(res.data.code==1){
            this.list = res.data.data
            this.allpage = res.data.last_page
          }else if(res.data.code==0){
            this.list = []
          }
        })
      },
      /*基础操作*/
      add () {
        this.boxISshow = true
        this.tankuang_title = '添加'
      },
      edit (id) {
        this.boxISshow = true
        this.tankuang_title = '编辑'
        this.axios.get('/index/show_dknow?id='+id).then((res)=>{
          if(res.data.code==1){
            let datas = res.data.data
            this.time = datas.cost_time
            this.type = datas.cost_type
            this.money = datas.cost_money
            this.pid = id
          }else{
            this.list = []
            Toast(res.data.msg)
            return
          }
        })
      },
      save () {
        if(!this.time||!this.type||!this.money){
          Toast('内容必须填写完整！！')
          return
        }
        let index = new Date(this.time).toLocaleDateString().lastIndexOf('/')
        this.time = new Date(this.time).toLocaleDateString().replace(/\//ig,"-").slice(0,index)
        this.axios.post('/index/save_dknow',{
          id:this.pid||'',
          cost_time:this.time,
          cost_type:this.type,
          cost_money:this.money,
          pro_id:this.id
        }).then((res)=>{
          if(res.data.code==1){
            this.axios.get('/index/dknow_list?pro_id='+this.id+'&page='+this.current).then((res)=>{
              if(res.data.code==1){
                this.list = res.data.data
                this.allpage = res.data.last_page
              }else{
                this.list = []
              }
            })
            this.boxISshow = false
            this.time = ''
            this.type = ''
            this.money = ''
            this.pid = null
          }else{
            Toast(res.data.msg)
            return
          }
        })
      },
      cancelSave () {
        this.boxISshow = !this.boxISshow
        this.time = ''
        this.type = ''
        this.money = ''
        this.pid = null
      },
      closeBox () {
        this.boxISshow = !this.boxISshow
        this.time = ''
        this.type = ''
        this.money = ''
        this.pid = null
      },
      cancelDel () {
        this.delISshow = !this.delISshow
      },
      tankuangISshowBTN () {
        this.tankuangISshow = !this.tankuangISshow
      },
      /*同步数据*/
      synchronization(){
        this.isnodataShow = true
      },
      closeSynchronizationBOX(){
        this.isnodataShow = false
      }
    }
  }
</script>

<style scoped>
  @import '../../assets/public.css';
  @import '//at.alicdn.com/t/font_749860_je5eki3tevb.css';
  .datepicker{
    width:210px;
  }
  .add_tankuang{
    width:594px;
    height:396px;
  }
  .common_tankuang_content{
    padding:40px 0;
    display: block;
  }
  .common_tankuang_content>li{
    flex:1;
  }
  .common_tankuang_content input{
    width:210px!important;
  }
  .common_tankuang_content select{
    width:210px!important;
  }
  .common_tankuang_content span{
    width:200px;
  }
  .tankuang{
    width:300px;
    height:157px;
    padding:30px;
  }
  .tankuang_ok_btn{
    padding:6px 15px;
    font-size:16px;
    width:110px;
  }

  /*那个同步数据弹出来的框框*/
  #nodatabox{
    margin:auto;
    width:344px;
    height:210px;
    background-color: #fff;
    border-radius: 7px;
    box-shadow: 2px 2px 3px #e6e6e6;
    padding:60px 60px 0;
    position: absolute;
    top:0;
    left: 0;
    right: 0;
    bottom: 0;
    box-sizing: border-box;
    border: 1px solid #ddd;
  }
  #nodatabox>i{
    position: absolute;
    right:10px;
    top:10px;
    cursor: pointer;
  }
  .nodata_text{
    color: #666;
    font-size: 14px;
  }
  .nodata_text_link{
    color: #4797D4;
  }
  .nodata_link_btn{
    display: block;
    width:100px;
    height:34px;
    margin:35px auto 0;
    background-color: #4797D4;
    color: #fff;
    font-size: 16px;
    text-align: center;
    line-height: 34px;
    border-radius: 4px;
    box-sizing: border-box;
    border: none;
    cursor: pointer;
  }

</style>
